<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>{function}</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
<link rel="home" href="index.html" title="Smarty 3 Manual">
<link rel="up" href="language.builtin.functions.html" title="Chapter 7. Built-in Functions">
<link rel="prev" href="language.function.foreach.html" title="{foreach},{foreachelse}">
<link rel="next" href="language.function.if.html" title="{if},{elseif},{else}">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">{function}</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="language.function.foreach.html">Prev</a> </td>
<th width="60%" align="center">Chapter 7. Built-in Functions</th>
<td width="20%" align="right"> <a accesskey="n" href="language.function.if.html">Next</a>
</td>
</tr>
</table>
<hr>
</div>
<div class="sect1" title="{function}">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="language.function.function"></a>{function}</h2></div></div></div>
<p>
  <code class="varname">{function}</code> is used to create functions within a template
  and call them just like a plugin function. Instead of writing a plugin that 
  generates presentational content, keeping it in the template is often a more 
  manageable choice. It also simplifies data traversal, such as deeply nested menus. 
  </p>
<div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Note</h3>
<p>
   Template functions are defined global. Since the Smarty compiler is a single-pass compiler,
   The <a class="link" href="language.function.call.html" title="{call}"><code class="varname">{call}</code></a>
   tag must be used to call a template function defined externally from the given template. 
   Otherwise you can directly use the function as <code class="literal">{funcname ...}</code> in the template.
  </p>
</div>
<div class="itemizedlist"><ul class="itemizedlist" type="disc">
<li class="listitem"><p>
    The <code class="varname">{function}</code> tag must have
    the  <em class="parameter"><code>name</code></em> attribute
    which contains the the name of the template function.
    A tag with this name can be used to call the template function.
   </p></li>
<li class="listitem"><p>
  Default values for variables can be passed to the template function as
  <a class="link" href="language.syntax.attributes.html" title="Attributes">attributes</a>.
 Like in PHP function declarations you can only use scalar values as default.
 The default values can be overwritten when the template function is being
 called.
 </p></li>
<li class="listitem"><p>
  You can use all variables from the calling template inside the template function. 
  Changes to variables or new created variables inside the template function
  have local scope and are not visible inside the calling template after the 
  template function is executed.
  </p></li>
</ul></div>
<p><span class="bold"><strong>Attributes:</strong></span></p>
<div class="informaltable"><table border="1">
<colgroup>
<col align="center">
<col align="center">
<col align="center">
<col align="center">
<col>
</colgroup>
<thead><tr>
<th align="center">Attribute Name</th>
<th align="center">Type</th>
<th align="center">Required</th>
<th align="center">Default</th>
<th>Description</th>
</tr></thead>
<tbody>
<tr>
<td align="center">name</td>
<td align="center">string</td>
<td align="center">Yes</td>
<td align="center"><span class="emphasis"><em>n/a</em></span></td>
<td>The name of the template function</td>
</tr>
<tr>
<td align="center">[var ...]</td>
<td align="center">[var type]</td>
<td align="center">No</td>
<td align="center"><span class="emphasis"><em>n/a</em></span></td>
<td>default variable value to pass local to the template function</td>
</tr>
</tbody>
</table></div>
<div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Note</h3>
<p>
  You can pass any number of parameter to the template function when it is called. The parameter variables
  must not be declared in the <code class="literal">{funcname ...}</code> tag unless you what to use default values.
  Default values must be scalar and can not be variable. Variables must be passed when the template is called.
 </p>
</div>
<div class="example">
<a name="id373317"></a><p class="title"><b>Example 7.43. Recursive menu {function} example</b></p>
<div class="example-contents">
<pre class="programlisting">

{* define the function *}
{function name=menu level=0}
{function menu level=0}          {* short-hand *}
  &lt;ul class="level{$level}"&gt;
  {foreach $data as $entry}
    {if is_array($entry)}
      &lt;li&gt;{$entry@key}&lt;/li&gt;
      {menu data=$entry level=$level+1}
    {else}
      &lt;li&gt;{$entry}&lt;/li&gt;
    {/if}
  {/foreach}
  &lt;/ul&gt;
{/function}

{* create an array to demonstrate *}
{$menu = ['item1','item2','item3' =&gt; ['item3-1','item3-2','item3-3' =&gt;
['item3-3-1','item3-3-2']],'item4']}

{* run the array through the function *}
{menu data=$menu}

  </pre>
<p>
  Will generate the following output
 </p>
<pre class="programlisting">

* item1
* item2
* item3
      o item3-1
      o item3-2
      o item3-3
            + item3-3-1
            + item3-3-2
* item4

  </pre>
</div>
</div>
<br class="example-break"><p>
  See also
  <a class="link" href="language.function.call.html" title="{call}"><code class="varname">{call}</code></a>
 </p>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="language.function.foreach.html">Prev</a> </td>
<td width="20%" align="center"><a accesskey="u" href="language.builtin.functions.html">Up</a></td>
<td width="40%" align="right"> <a accesskey="n" href="language.function.if.html">Next</a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">{foreach},{foreachelse} </td>
<td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
<td width="40%" align="right" valign="top"> {if},{elseif},{else}</td>
</tr>
</table>
</div>
</body>
</html>
